<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>平台组件管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <script type="text/javascript"
            src="../lib/vue/jquery.min.js"></script>
    <script type="text/javascript" src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <script type="text/javascript" src="../js/global.js"></script>
    <script type="text/javascript" src="../js/store.js"></script>
    <script type="text/javascript" src="../js/cookies.js"></script>
    <script src="../lib/vue/vue.min.js"></script>
    <script src="../lib/vue/axios.min.js"></script>
</head>

<body>
<div class="x-body layui-anim layui-anim-up" id="managerAdd">
    <form class="layui-form">
        <div class="layui-form-item" hidden>
            <div class="layui-input-inline">
                <input type="text" id="L_managerId" name="id" required=""
                       lay-verify=""
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item" hidden>
            <div class="layui-input-inline">
                <input type="text" id="L_psw" name="password" required=""
                       lay-verify=""
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_userName" class="layui-form-label">
                <span class="x-red">*</span>用户名
            </label>
            <div class="layui-input-inline" style="width: 300px;">
                <input type="text" id="L_userName" name="username" required=""
                       lay-verify="userName"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>非空且唯一
            </div>
        </div>
        <div class="layui-form-item">
            <label for='L_managerFlag' class="layui-form-label">
                <span class="x-red">*</span>角色
            </label>
            <!--3：组件管理员 4:超级管理员-->
            <div class="layui-input-inline">
                <select id="L_managerFlag" name="managerFlag" lay-filter='managerFlag'>
                    <option value=''>--请选择角色--
                    <option value='3' id="L_componentManager">组件管理员
                    <option value='4' id="L_superManager">超级管理员
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px">
                <span class="x-red">*</span>负责组件
            </label>
            <div class="layui-input-inline" style="width: 400px;" id="L_componentIds">

            </div>
        </div>
        <div class="layui-form-item" style="padding-top: 100px">
            <label class="layui-form-label">
            </label>
            <button class="layui-btn" lay-filter="edit" lay-submit="">
                保存
            </button>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'layer'], function () {
            $ = layui.jquery
            let form = layui.form
                , layer = layui.layer;
            //获取选中的数据 进行格式转换，并加载到form表单中
            var editData = parent.$("#editManager").val();
            var editArray = JSON.parse(editData);
            var componentIds = editArray.componentIds;
            $('#L_psw').val(editArray.password);

            var strID;
            $('#L_managerId').val(editArray.id);
            $('#L_userName').val(editArray.username);
            //渲染下拉框
            if (editArray.managerFlag === 3) {
                $('#L_componentManager').attr("selected", "")
            }
            form.render();//下拉菜单渲染 把内容加载进去

            let componentArray;
            //获取组件下拉表
            $.get(_hostUrl + '/cmsComponents', function (res) {
                if (res.responseCode != 200) {
                    layer.msg(res.responseMsg, {icon: 2, time: 1500});
                    return
                }
                componentArray = res.data;
                if (!componentArray || componentArray.length == 0) {
                    layer.msg("还未创建组件信息!")
                } else {
                    for (let i = 0; i < componentArray.length; i++) {
                        strID = res.data[i].id.toString();
                        if (componentIds == null || componentIds == "") {
                            $('#L_componentIds').append("<input  type='checkbox' lay-filter='componentIds' name='componentIds'   value=" + res.data[i].id + " title =" + res.data[i].componentName + ">" + "<br/>")
                        } else {
                            if (componentIds.indexOf(strID) == -1) {
                                $('#L_componentIds').append("<input type='checkbox' lay-filter='componentIds' name='componentIds' value=" + res.data[i].id + " title =" + res.data[i].componentName + ">" + "<br/>")
                            } else {
                                $('#L_componentIds').append("<input checked type='checkbox' lay-filter='componentIds' name='componentIds'   value=" + res.data[i].id + " title =" + res.data[i].componentName + ">" + "<br/>")
                            }
                        }
                    }
                    $('#L_componentIds').val(parseInt(getUrlKey('id')));
                    form.render();
                }
            });
            //自定义验证规则
            form.verify({
                userName: function (value) {
                    if (value.length < 1) {
                        return '用户名不可为空'
                    }
                }
            });
            //监控组件列表选择事件
            form.on('checkbox(componentIds)', function () {
                //拼接id
                componentIds = getCheckboxValue('componentIds')
            })
            //监听提交
            form.on('submit(edit)', function (data) {
                data.field.componentIds = componentIds;
                $.putForm(_hostUrl + "/cmsManager", data.field, function (res) {
                    if (res.responseCode == 201) {
                        layer.msg('修改管理员成功', {
                            icon: 1,
                            time: 2000
                        }, function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name)
                            //关闭当前frame
                            parent.layer.close(index)
                            var table = parent.window.layui.table;
                            table.reload('managerList')
                        });
                    } else {
                        layer.msg(res.responseMsg, {icon: 2, time: 1500})
                    }
                }, 'json');
                return false
            });
        }
    )
</script>
</body>

</html>